home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-08 | 3.7 KB | 141 lines | [TEXT/PJMM] |
- {This variant of Assignment6 uses the Add-on lib to simplify the sprite movement.}
- {The routine HandleSprite gets a lot simpler this way.}
-
- program Assignment6;
- uses
- {$ifc UNDEFINED THINK_PASCAL}
- Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, OSEvents, {}
- {$endc}
- SAT, SATAddOnLib;
-
- const
- kSpeed = 5;
- var
- ignore: SpritePtr;
- theSound: Handle;
- score: Longint;
- startTime: Longint;
- doneFlag: Boolean;
-
- procedure HandleSprite (me: SpritePtr);
- var
- event: EventRecord;
- begin
- {Now hold on to the hat! I'm using GetOSEvent to get key down events.}
- {the keydowns then affect the speed variable in the sprite. That speed}
- {is added to the position. Finally, I check the position against the screen}
- {borders, and modify the speed in case I reach a border.}
-
- if GetOSEvent(keyDownMask, event) then
- if event.what = keyDown then
- case char(BAnd(event.message, charCodeMask)) of
- 'a':
- me^.speed.h := me^.speed.h - 1;
- 's':
- me^.speed.h := me^.speed.h + 1;
- 'w':
- me^.speed.v := me^.speed.v - 1;
- 'z':
- me^.speed.v := me^.speed.v + 1;
- end;
-
- {Now look how much shorter the rest of HandleSprite got!}
- MoveSprite(me);
- if KeepOnScreen(me) then
- ;
- end; {HandleSprite}
-
- procedure SetupSprite (me: SpritePtr);
- begin
- me^.task := @HandleSprite;
- me^.face := SATGetFace(128);
- me^.speed := Point(0);
- SetRect(me^.hotRect, 0, 0, 32, 32);
- end; {SetupSprite}
-
- procedure SetupTarget (me: SpritePtr);
- forward;
-
- procedure HandleTarget (me: SpritePtr);
- begin
- {The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead}
- {of a hard-coded constant.}
-
- {This wasn't simplified as much since this sprite has such a simple behavior.}
- MoveSprite(me);
- if KeepOnScreen(me) then
- ;
- end; {HandleTarget}
-
- procedure HitTarget (me, him: SpritePtr);
- var
- savePort: SATPort;
- r: Rect;
- begin
- if him^.task = @HandleSprite then {Check what we hit!}
- begin
- me^.task := nil;
- ignore := SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), @SetupTarget);
- {We could also re-use the old sprite for a new one, if we like.}
- SATSoundPlay(theSound, 1, true);
-
- {Add to the score}
- score := score + 1;
-
- {Draw the score on the screen.}
- SATGetPort(savePort); {Save port and device!}
- SATSetPortBackScreen;
- SetRect(r, 100, 0, 100 + stringWidth(stringof('Caught: ', score : 1)), 15);
- EraseRect(r);
- MoveTo(r.left, r.bottom - 3);
- DrawString(stringof('Caught: ', score : 1));
- SATBackChanged(r);
- SATSetPort(savePort); {Always restore!}
-
- if score = 10 then
- doneFlag := true;
- end;
- end; {HitTarget}
-
- procedure SetupTarget (me: SpritePtr);
- begin
- me^.task := @HandleTarget;
- me^.hitTask := @HitTarget;
- me^.face := SATGetFace(129);
- SetRect(me^.hotRect, 0, 0, 32, 32);
- me^.speed.h := kSpeed;
- end; {SetupTarget}
-
- const
- kTicksPerFrame = 2;
- var
- t: Longint;
-
- begin
- {If we don't use Think Pascal, we must make standard inits ourselves.}
- {$ifc UNDEFINED THINK_PASCAL}
- SATInitToolbox;
- {$endc}
-
- SATConfigure(false, kVPositionSort, kForwardCollision, 32);
- SATInit(128, 129, 478, 302);
- ignore := SATNewSprite(0, 200, 200, @SetupSprite);
- ignore := SATNewSprite(0, 0, SATRand(gSAT.offSizeV), @SetupTarget);
- theSound := SATGetNamedSound('TestSound');
- score := 0;
- HideCursor;
- startTime := TickCount;
- doneFlag := false;
- while not Button and not doneFlag do
- begin
- t := TickCount;
- SATRun(true);
- while TickCount < t + kTicksPerFrame do
- ;
- end;
- ShowCursor;
- {Extremely simple result report. A real game should, of course, display this in the game}
- {window, in a prettier way, perhaps with a high score list, etc.}
- SATReportStr(stringof('Time to catch ', score : 1, ' disks: ', (TickCount - startTime) div 60 : 1, ' seconds.'));
- SATSoundShutup;
- end.